p.s. Allison Horstβs R illustrations are amaaaazing and I will be using them throughout.
2021/03/09
p.s. Allison Horstβs R illustrations are amaaaazing and I will be using them throughout.
Learn how to use R Markdown to combine writing and code
π Create new R project
π Create, edit, and compile .Rmd file
π Create & edit helper.R file to feed to your .Rmd
Simple syntax that allows you to add tags to plain text to format it
Originally designed to be HTML replacement
“Minimalist writing system”
Lifehacker: What is markdown and why is it better for my to do lists and notes?
βοΈ Very useful for writing summary reports, articles, etc.
YAML (rhymes with camel): The header that tells R Markdown how to generate your document. Indentation and spacing are very important.
Permits the following to happen when you knit:
YAML: “YAML Ainβt Markup Language”
Basic:
title: "Untitled" author: "Thea Knowles" date: '2018-02-18' output: word_document
More optionsβ¦
title: "Changes in voice acoustics along a speech rate continuum
in Parkinson's disease"
author: "Thea Knowles, Scott G. Adams, Mandar Jog"
date: Last updated `r Sys.Date()`
output:
bookdown::word_document2:
reference_docx: "../rmd_templates/custom_reference.docx"
redoc::redoc:
highlight_outputs: FALSE
margins: 1
line_numbers: FALSE
bibliography: references.bib
csl: "csl_files/apa7.csl"
Even more optionsβ¦
Different options for:
Chunks are sections that will include R code. By setting defaults at the beginning of your document, you can specify what you want most of your chunks to do.
In each chunk, you can specify options in the form tag=value in the chunk header.
include is set to FALSE, indicating that we donβt want the contents of this chunk included in the outputFirst, insert a new R chunk by
Alt + Cmd/Ctrl + i ORCode >> Insert Chunk from the R Studio menu```{r}
x <- 10
```
```{r my-chunk}
x <- 10
```
β οΈ Chunk labels CANNOT contain spaces, underscores, or special characters, but CAN contain hyphens.
```{r my-chunk, echo = FALSE}
x <- 10
```
In this example, we set the echo option:
echo = TRUE: show the code in the rendered Rmd documentecho = FALSE: donβt show the code in the output, but DO run it in the backgroundβ‘οΈRMarkdown cheat sheet
β‘οΈRStudio lesson
β‘οΈKnitr book chapter
Letβs say you want to set default behavior for all of your code chunks. You can do this using the knitr package in a special chunk at the beginning of your document.
When you create a new Rmarkdown document from the default template, this is included for you:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This chunk provides the following information for “knitting” the document:
echo = TRUE for all chunks (you can still set echo = FALSE in individual chunks later if you want)Markdown: set of conventions for editing plain text.
Write as you normally would in a text editor or word processor, but you signal text formatting with certain characters (next slide).
Markdown (which is distinguished from markUP language) is designed to be
Tables written like this:
First Header | Second Header ------------- | ------------- Content Cell | Content Cell Content Cell | Content Cell
See remedy and beautifyR R packages/RStudio addins for dealing with markdown tables
Data frames can be turned into tables without manual modification
kable and flextable R packagesIn the Rmarkdown document you created from the default templateβ¦
Delete everything after Line 12 (after the setup code chunk)
Add a new header
Add some plain text
Add some bold text
Add a code chunk that includes x <- 5 and x
Include a line that says:
The value of x is `r x`
Compile! (“Knit”): Cmd/Ctrl + K or with Knit button.
helper.R file in my RMarkdown document and use code chunks to polish figures/tableshelper.RFirst: make sure simulated_vot_data.csv is in your project folder (same level as the .RProject). Then:
helper.R in your project folder (same level as your .RProject for now)Try to run the code in the R console.
π We can pause to diagnose any errors people run into!
helper.R in your RMarkdown documentOpen your .Rmd file and insert a new code chunk below the setup chunk
```{r source-helper}
source("helper.R")
min(vot$VOT)
max(vot$VOT)
```
Knit the .Rmd document. What do you see?
π We can pause to diagnose any errors people run into!
Below your R chunk, add the following lines:
The mean VOT is `r vot_m`.
The mean VOT is `r min(vot$VOT)` and the max VOT is `r max(vot$VOT)`.
Add another code chunk:
```{r vot-distribution}
hist(vot$VOT)
```